﻿@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager


<MDCard Class="@($"pa-4 mb-3  {(IsChild?"elevation-2":"")}")" Color="@(IsChild?"primary":"background")">
    <div>
        <CnGalWebSite.Shared.MasaComponent.Shared.Cards.Users.InfoCard Model="Model.UserInfor" Outline Class="mb-2"/>
        <div class="mb-2">
            <CnGalWebSite.Shared.MasaComponent.Shared.Components.Markdown.MarkdownViewTip IsNeedHighlight="true" Text="@(ReplyTip+Model.Text)" />
        </div>
         <div class="mb-2 d-flex" style="justify-content: flex-end;">
             <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaIcon Type="IconType.DeterminedTime"/>
             @Model.CommentTime.ToString("yyyy-MM-dd HH:mm")
         </div>
        <AuthorizeView>
            <Authorized>
                <div style=" display: flex; justify-content: flex-end; ">
                    @if (IsReplying)
                    {
                        <CnGalWebSite.Components.Buttons.MasaButton Small Text="取消回复" OnClick="OnUnReplyClick" Icon="@IconType.Close.ToIconString()" Rounded />
                    }
                    else
                    {
                        <CnGalWebSite.Components.Buttons.MasaButton Small Text="回复" OnClick="OnReplyClick" Icon="@IconType.Comment.ToIconString()" IsAsync Rounded />
                    }


                    @if (CanDeleted)
                    {
                        <CnGalWebSite.Components.Buttons.MasaButton Small Text="删除" OnClick="OnDeleteClick" Icon="@IconType.Delete.ToIconString()" IsAsync Rounded Class="ms-4" />
                    }
                </div>
            </Authorized>
        </AuthorizeView>
    </div>
</MDCard>
    @if (IsReplying)
    {
        <div class="@($"mb-3 {(Deepth >= MaxDeepth?"":"ms-4")}")">
            <ReplyCommentTip ObjectId="@ObjectId" IsChild="true" Type="CommentType.ReplyComment" OnRefresh="OnRely"></ReplyCommentTip>

        </div>
    }
    @foreach (var item in Model.InverseParentCodeNavigation)
    {
        <div class="@($"mb-3 {(Deepth >= MaxDeepth?"":"ms-4")}")">
            <CommentPartTip Model="item" IsChild="true" OnRefresh="OnRely_Comment"
                            Deepth="Deepth+1" ParentCommentModel="@Model" CanDeleted="@CanDeleted"></CommentPartTip>
            </div>
    }

@code {
    [Parameter]
    public CommentViewModel Model { get; set; }

    [Parameter]
    public bool IsChild { get; set; }

    [Parameter]
    public EventCallback OnRefresh { get; set; }

    [Parameter]
    public CommentViewModel ParentCommentModel { get; set; }

    public long LastId { get; set; }

    [Parameter]
    public bool CanDeleted { get; set; }

    [Parameter]
    public int Deepth { get; set; }

    private int MaxDeepth = 3;

    bool isRead = false;

    private bool IsReplying { get; set; }

    private string StringMarked { get; set; }

    public string ReplyTip { get { return Deepth > MaxDeepth ? $"<p>回复<a href=\"/space/index/{ParentCommentModel.UserInfor.Id}\">@{ParentCommentModel.UserInfor.Name}</a></p>\n" : ""; } }


    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    private string ObjectId
    {
        get
        {
            return Model.Id.ToString();
        }
    }

    public void OnReplyClick()
    {
        IsReplying = true;
        StateHasChanged();
    }

    public void OnUnReplyClick()
    {
        IsReplying = false;
        StateHasChanged();
    }

    public void OnLoginClick()
    {
        NavigationManager.NavigateTo("/account/longin");
    }

    public async Task OnRely()
    {
        //这是回复组件的事件回调
        IsReplying = false;
        await OnRefresh.InvokeAsync();
    }

    public async Task OnRely_Comment()
    {
        //这是级联传递的事件回调
        await OnRefresh.InvokeAsync();
    }

    int count = 0;

    private async Task OnDeleteClick()
    {

        //调用API
        try
        {
            count++;
            if (count < 2)
            {
                await PopupService.ToastAsync("你确定要删除这条评论吗？确认删除请再次点击删除按钮", AlertTypes.Info);
                return;
            }

            var obj = await _httpService.PostAsync<HiddenCommentModel, Result>("api/comments/UserHiddenComment", new HiddenCommentModel { Ids = new long[] { Model.Id }, IsHidden = true });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync("删除评论失败", AlertTypes.Info);
            }
            else
            {

                await PopupService.ToastAsync("删除评论成功", AlertTypes.Success);
                await OnRefresh.InvokeAsync();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "删除评论失败");
        }
    }
}
